package com.sbpm.crmbpm.client.grid;

import com.google.gwt.core.client.GWT;
import com.sbpm.crmbpm.client.providers.TaskInfoProperties;
import com.sbpm.crmbpm.shared.data.TaskInfo;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;

import java.util.ArrayList;
import java.util.List;

/**
 * @deprecated
 * Created by U_M0PQL on 19.10.2014.
 */
public class TaskGrid
{
    private static final TaskInfoProperties props = GWT.create(TaskInfoProperties.class);


    public Grid createTaskGrid( List<TaskInfo> tasks )
    {
        //final NumberFormat number = NumberFormat.getFormat("0.00");

        ColumnConfig<TaskInfo, Long> taskIdCol = new ColumnConfig<TaskInfo, Long>(props.id(), 50, "TaskId");

        ColumnConfig<TaskInfo, String> nameCol = new ColumnConfig<TaskInfo, String>(props.name(), 300, "Name");
        ColumnConfig<TaskInfo, String> processIdCol = new ColumnConfig<TaskInfo, String>(props.processId(), 200, "Process Id");
        ColumnConfig<TaskInfo, String> statusCol = new ColumnConfig<TaskInfo, String>(props.status(), 75, "Status");
        ColumnConfig<TaskInfo, Integer> priorityCol = new ColumnConfig<TaskInfo, Integer>(props.priority(), 75, "Priority");
        ColumnConfig<TaskInfo, Long> processInstanceIdCol = new ColumnConfig<TaskInfo, Long>(props.processInstanceId(), 140, "ProcessInstanceId");

        /*ColumnConfig<TaskInfo, Double> changeCol = new ColumnConfig<TaskInfo, Double>(props.change(), 100, "Change");
        changeCol.setCell(new AbstractCell<Double>() {

            @Override
            public void render(Context context, Double value, SafeHtmlBuilder sb) {
                String style = "style='color: " + (value < 0 ? "red" : "green") + "'";
                String v = number.format(value);
                sb.appendHtmlConstant("<span " + style + " qtitle='Change' qtip='" + v + "'>" + v + "</span>");
            }
        });*/

        /*ColumnConfig<TaskInfo, Date> lastTransCol = new ColumnConfig<TaskInfo, Date>(props.lastTrans(), 100, "Last Updated");
        lastTransCol.setCell(new DateCell(DateTimeFormat.getFormat("MM/dd/yyyy")));*/

        List<ColumnConfig<TaskInfo, ?>> l = new ArrayList<ColumnConfig<TaskInfo, ?>>();
        l.add(taskIdCol);
        l.add(nameCol);
        l.add(processIdCol);
        l.add(statusCol);
        l.add(priorityCol);
        l.add(processInstanceIdCol);
        ColumnModel<TaskInfo> cm = new ColumnModel<TaskInfo>(l);

        ListStore<TaskInfo> store = new ListStore<TaskInfo>(props.key());
        store.addAll(tasks);

        /*root = new ContentPanel();
        root.setHeadingText("Basic Grid");
        root.getHeader().setIcon(ExampleImages.INSTANCE.table());
        root.setPixelSize(600, 300);
        root.addStyleName("margin-10");

        ToolButton info = new ToolButton(ToolButton.QUESTION);
        ToolTipConfig config = new ToolTipConfig("Example Info", "This examples includes resizable panel, reorderable columns and grid state.");
        config.setMaxWidth(225);
        info.setToolTipConfig(config);
        root.addTool(info);

        final Resizable resizable = new Resizable(root, Dir.E, Dir.SE, Dir.S);
        root.addExpandHandler(new ExpandHandler() {
            @Override
            public void onExpand(ExpandEvent event) {
                resizable.setEnabled(true);
            }
        });
        root.addCollapseHandler(new CollapseHandler() {
            @Override
            public void onCollapse(CollapseEvent event) {
                resizable.setEnabled(false);
            }
        });*/

        final Grid<TaskInfo> grid = new Grid<TaskInfo>(store, cm);
//        grid.getView().setAutoExpandColumn(nameCol);
        grid.getView().setStripeRows(true);
        grid.getView().setColumnLines(true);
        grid.setBorders(false);

        grid.setColumnReordering(true);
//        grid.setStateful(false);
//        grid.setStateId("gridExample");

        return grid;

        /*GridStateHandlerr<TaskInfo> state = new GridStateHandler<TaskInfo>(grid);
        state.loadState();

        ToolBar toolBar = new ToolBar();
        toolBar.setEnableOverflow(false);
        toolBar.add(new LabelToolItem("Selection Mode: "));

        SimpleComboBox<String> type = new SimpleComboBox<String>(new StringLabelProvider<String>());
        type.setTriggerAction(TriggerAction.ALL);
        type.setEditable(false);
        type.setWidth(100);
        type.add("Row");
        type.add("Cell");
        type.setValue("Row");
        // we want to change selection model on select, not value change which fires on blur
        type.addSelectionHandler(new SelectionHandler<String>() {

            @Override
            public void onSelection(SelectionEvent<String> event) {
                boolean cell = event.getSelectedItem().equals("Cell");
                if (cell) {
                    CellSelectionModel<Stock> c = new CellSelectionModel<Stock>();
                    c.addCellSelectionChangedHandler(new CellSelectionChangedHandler<Stock>() {

                        @Override
                        public void onCellSelectionChanged(CellSelectionChangedEvent<Stock> event) {

                        }
                    });

                    grid.setSelectionModel(c);
                } else {
                    grid.setSelectionModel(new GridSelectionModel<Stock>());
                }
            }
        });
        type.addValueChangeHandler(new ValueChangeHandler<String>() {

            @Override
            public void onValueChange(ValueChangeEvent<String> event) {

            }
        });
        toolBar.add(type);

        VerticalLayoutContainer con = new VerticalLayoutContainer();
        root.setWidget(con);

        con.add(toolBar, new VerticalLayoutData(1, -1));
        con.add(grid, new VerticalLayoutData(1, 1));

        // needed to enable quicktips (qtitle for the heading and qtip for the
        // content) that are setup in the change GridCellRenderer
        new QuickTip(grid);*/
    }
}
